package oj_leetcode;

public class CountAndSay {
    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        String str = countAndSay(n - 1);
        char pre = str.charAt(0);
        int count = 0;
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char curr = str.charAt(i);
            if (curr == pre) {
                count++;
            } else {
                res.append(count).append(pre);
                pre = curr;
                count = 1;
            }
        }
        res.append(count).append(pre);
        return res.toString();
    }
}
